package com.yellowpage.omc.datacollect.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.omc.datacollect.data.DataVerify;
import com.yellowpage.omc.datacollect.service.DataVerifyService;

public class DataVerifyAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<DataVerify> dataVerifyList = new ArrayList<DataVerify>();
	private List<Map> resultList = new ArrayList<Map>();

	private DataVerify dataVerify;
	private DataVerifyService dataVerifyService;

	private String selectedId;
	private String[] selectedIds;
	private String message;
	private String inSingleMessage;

	private String query_dataVerifyId;
	private String query_dataCollectId;
	private String query_verifyResult;
	private String query_link;
	private String query_telType;
	private String query_tel;
	private String query_city;
	private String query_class01;
	private String query_class02;
	private String query_addr;
	private String query_fax;
	private String query_tel02;
	private String query_createUser;
	private String query_createTime;
	private String query_lastUpdateUser;
	private String query_lastUpdateTime;
	private String query_note;
	private String query_name;
	private String query_icon;
	private String query_info;
	private String query_status;
	
	private String query_batchNum;
	private String query_seq;

	private Map statusMap;
	private Map optTypeMap;

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.dataVerify = dataVerifyService.getDataVerify(Long
					.parseLong(selectedId));
		}

		statusMap = dataVerifyService.getStatusMap();
		optTypeMap = dataVerifyService.getOptTypeMap();
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		
		queryMap.put("batchNum", query_batchNum);
		queryMap.put("seq", query_seq);
		
		queryMap.put("dataVerifyId", query_dataVerifyId);
		queryMap.put("dataCollectId", query_dataCollectId);
		queryMap.put("verifyResult", query_verifyResult);
		queryMap.put("link", query_link);
		queryMap.put("telType", query_telType);
		queryMap.put("tel", query_tel);
		queryMap.put("city", query_city);
		queryMap.put("class01", query_class01);
		queryMap.put("class02", query_class02);
		queryMap.put("addr", query_addr);
		queryMap.put("fax", query_fax);
		queryMap.put("tel02", query_tel02);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createTime", query_createTime);
		queryMap.put("lastUpdateUser", query_lastUpdateUser);
		queryMap.put("lastUpdateTime", query_lastUpdateTime);
		queryMap.put("note", query_note);
		queryMap.put("name", query_name);
		queryMap.put("icon", query_icon);
		queryMap.put("info", query_info);
		queryMap.put("status", query_status);

		resultList = dataVerifyService.queryDataVerifyByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		dataVerifyService.saveDataVerify(dataVerify);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (dataVerify.getVerifyResult().length() == 0) {
			this.addActionError("** VerifyResult 不能为空 !");
		}
		if (dataVerify.getLink().length() == 0) {
			this.addActionError("** Link 不能为空 !");
		}
		if (dataVerify.getTelType().length() == 0) {
			this.addActionError("** TelType 不能为空 !");
		}
		if (dataVerify.getTel().length() == 0) {
			this.addActionError("** Tel 不能为空 !");
		}
		if (dataVerify.getCity().length() == 0) {
			this.addActionError("** City 不能为空 !");
		}
		if (dataVerify.getClass01().length() == 0) {
			this.addActionError("** Class01 不能为空 !");
		}
		if (dataVerify.getClass02().length() == 0) {
			this.addActionError("** Class02 不能为空 !");
		}
		if (dataVerify.getAddr().length() == 0) {
			this.addActionError("** Addr 不能为空 !");
		}
		if (dataVerify.getFax().length() == 0) {
			this.addActionError("** Fax 不能为空 !");
		}
		if (dataVerify.getTel02().length() == 0) {
			this.addActionError("** Tel02 不能为空 !");
		}
		if (dataVerify.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (dataVerify.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
		if (dataVerify.getIcon().length() == 0) {
			this.addActionError("** Icon 不能为空 !");
		}
		if (dataVerify.getInfo().length() == 0) {
			this.addActionError("** Info 不能为空 !");
		}
		if (dataVerify.getStatus().length() == 0) {
			this.addActionError("** Status 不能为空 !");
		}

		this.setValidatorResult("/omc/datacollect/DataVerifyAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		dataVerify = dataVerifyService.getDataVerify(id);

		return RESULT_EDIT;
	}

	public String update() {

		dataVerifyService.updateDataVerify(dataVerify);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (dataVerify.getVerifyResult().length() == 0) {
			this.addActionError("** VerifyResult 不能为空 !");
		}
		if (dataVerify.getLink().length() == 0) {
			this.addActionError("** Link 不能为空 !");
		}
		if (dataVerify.getTelType().length() == 0) {
			this.addActionError("** TelType 不能为空 !");
		}
		if (dataVerify.getTel().length() == 0) {
			this.addActionError("** Tel 不能为空 !");
		}
		if (dataVerify.getCity().length() == 0) {
			this.addActionError("** City 不能为空 !");
		}
		if (dataVerify.getClass01().length() == 0) {
			this.addActionError("** Class01 不能为空 !");
		}
		if (dataVerify.getClass02().length() == 0) {
			this.addActionError("** Class02 不能为空 !");
		}
		if (dataVerify.getAddr().length() == 0) {
			this.addActionError("** Addr 不能为空 !");
		}
		if (dataVerify.getFax().length() == 0) {
			this.addActionError("** Fax 不能为空 !");
		}
		if (dataVerify.getTel02().length() == 0) {
			this.addActionError("** Tel02 不能为空 !");
		}
		if (dataVerify.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (dataVerify.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
		if (dataVerify.getIcon().length() == 0) {
			this.addActionError("** Icon 不能为空 !");
		}
		if (dataVerify.getInfo().length() == 0) {
			this.addActionError("** Info 不能为空 !");
		}
		if (dataVerify.getStatus().length() == 0) {
			this.addActionError("** Status 不能为空 !");
		}
		this.setValidatorResult("/omc/datacollect/DataVerifyUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.dataVerifyService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	public String inSingle() {
		log.error("inSingle in...............................");

		for (String id : selectedIds) {
			log.error("id is " + id);
			dataVerify = dataVerifyService.getDataVerify(Long.parseLong(id));

			dataVerify.setLastUpdateTime(new Date());
			dataVerify.setLastUpdateUser(loginUser.getUserId());
			
			if("PENDING".equals(dataVerify.getStatus())){
				dataVerifyService.inSingle(dataVerify);
			}else{
				inSingleMessage = inSingleMessage + " " + dataVerify.getTel() + "未入库";
			}
		}

		inSingleMessage = inSingleMessage + "入库成功";

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setDataVerify(DataVerify dataVerify) {
		this.dataVerify = dataVerify;
	}

	public DataVerify getDataVerify() {
		return dataVerify;
	}

	public void setDataVerifyService(DataVerifyService dataVerifyService) {
		this.dataVerifyService = dataVerifyService;
	}

	public List<DataVerify> getDataVerifyList() {
		return dataVerifyList;
	}

	public void setDataVerifyList(List<DataVerify> dataVerifyList) {
		this.dataVerifyList = dataVerifyList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_dataVerifyId() {
		return this.query_dataVerifyId;
	}

	public void setQuery_dataVerifyId(String dataVerifyId) {
		this.query_dataVerifyId = dataVerifyId;
	}

	public String getQuery_dataCollectId() {
		return this.query_dataCollectId;
	}

	public void setQuery_dataCollectId(String dataCollectId) {
		this.query_dataCollectId = dataCollectId;
	}

	public String getQuery_verifyResult() {
		return this.query_verifyResult;
	}

	public void setQuery_verifyResult(String verifyResult) {
		this.query_verifyResult = verifyResult;
	}

	public String getQuery_link() {
		return this.query_link;
	}

	public void setQuery_link(String link) {
		this.query_link = link;
	}

	public String getQuery_telType() {
		return this.query_telType;
	}

	public void setQuery_telType(String telType) {
		this.query_telType = telType;
	}

	public String getQuery_tel() {
		return this.query_tel;
	}

	public void setQuery_tel(String tel) {
		this.query_tel = tel;
	}

	public String getQuery_city() {
		return this.query_city;
	}

	public void setQuery_city(String city) {
		this.query_city = city;
	}

	public String getQuery_class01() {
		return this.query_class01;
	}

	public void setQuery_class01(String class01) {
		this.query_class01 = class01;
	}

	public String getQuery_class02() {
		return this.query_class02;
	}

	public void setQuery_class02(String class02) {
		this.query_class02 = class02;
	}

	public String getQuery_addr() {
		return this.query_addr;
	}

	public void setQuery_addr(String addr) {
		this.query_addr = addr;
	}

	public String getQuery_fax() {
		return this.query_fax;
	}

	public void setQuery_fax(String fax) {
		this.query_fax = fax;
	}

	public String getQuery_tel02() {
		return this.query_tel02;
	}

	public void setQuery_tel02(String tel02) {
		this.query_tel02 = tel02;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_lastUpdateUser() {
		return this.query_lastUpdateUser;
	}

	public void setQuery_lastUpdateUser(String lastUpdateUser) {
		this.query_lastUpdateUser = lastUpdateUser;
	}

	public String getQuery_lastUpdateTime() {
		return this.query_lastUpdateTime;
	}

	public void setQuery_lastUpdateTime(String lastUpdateTime) {
		this.query_lastUpdateTime = lastUpdateTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_icon() {
		return this.query_icon;
	}

	public void setQuery_icon(String icon) {
		this.query_icon = icon;
	}

	public String getQuery_info() {
		return this.query_info;
	}

	public void setQuery_info(String info) {
		this.query_info = info;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public String getInSingleMessage() {
		return inSingleMessage;
	}

	public void setInSingleMessage(String inSingleMessage) {
		this.inSingleMessage = inSingleMessage;
	}

	public Map getStatusMap() {
		return statusMap;
	}

	public void setStatusMap(Map statusMap) {
		this.statusMap = statusMap;
	}

	public String getQuery_batchNum() {
		return query_batchNum;
	}

	public void setQuery_batchNum(String query_batchNum) {
		this.query_batchNum = query_batchNum;
	}

	public String getQuery_seq() {
		return query_seq;
	}

	public void setQuery_seq(String query_seq) {
		this.query_seq = query_seq;
	}

	public Map getOptTypeMap() {
		return optTypeMap;
	}

	public void setOptTypeMap(Map optTypeMap) {
		this.optTypeMap = optTypeMap;
	}

}
